//
//  YZXUserAccount.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/21.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit



class YZXUserAccount: NSObject, NSCoding{
 /// 用于获取用户的数据
    var access_token:String?
    
// access_token 的生命周期 单位是秒数 但是由于是基本类型的double的值 所以是不能使用可选类型的
    var expires_in:NSTimeInterval = 0{
        didSet{
            expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
//    接收用户的UID
    var uid:String?
    
    // 过期时期时间的计算
    var expiresDate:NSDate?
    
// 用户的名称
    var screen_name:String?
// 用户的头像
    var avatar_large:String?
    
    
    init(dic:[String:AnyObject]){
        super.init()
        // 使用KVC赋值
        setValuesForKeysWithDictionary(dic)
        
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    
    /**
    *  归档和解档相关
    */
    required init?(coder aDecoder: NSCoder) {
        super.init()
        // 解档 将数据沙盒目录中读取出来
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        uid = aDecoder.decodeObjectForKey("uid") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
    
    
}
